

jWEIBO = {};

jWEIBO.HttpStatus = WEIBOHttpStatus;

jWEIBO.Utilities = jQuery;

jWEIBO.Controller = jQuery;

jWEIBO.DataObject = jQuery;

jWEIBO.Connection = jQuery;

jWEIBO.API = WEIBO.Configuration.API;
/*-------------------------------------------------------------------------------*/
jWEIBO.Utilities.extend({
	Error: {
		canvasErrorMessageBoard : function(){
			ErrorMessageBoard = document.getElementById("ErrorMessageBoard");
			if(!ErrorMessageBoard){ 
				console.log("create");
				m = document.createElement("div");
				m.id = "ErrorMessageBoard";
				document.body.appendChild(m);
				ErrorMessageBoard = m;
			}else{
				console.log("not create");
			};
			return ErrorMessageBoard;
		},
		setErrorMessageBoard: function(m){
			ErrorMessageBoard = jWEIBO.Utilities.Error.canvasErrorMessageBoard();
			if (m != ""){
				console.log(m);
				jQuery(ErrorMessageBoard).html(m);
				jWEIBO.Utilities.Error.showErrorMessageBoard();
			}
		},
		showErrorMessageBoard : function(){
			ErrorMessageBoard = jWEIBO.Utilities.Error.canvasErrorMessageBoard();
			jQuery(ErrorMessageBoard).fadeIn("fast").fadeOut(10000);
		}
	}
});
/*-------------------------------------------------------------------------------*/

jWEIBO.Connection.extend({
	response : {
		onJsonPLoad : function(data){
			return (data);
		},
		
		onErrorLoad : function(xhr, textStatus, errorThrown){
			//console.log(xhr);
			errorMessage = jWEIBO.HttpStatus[xhr.status][1];
			jWEIBO.Utilities.Error.setErrorMessageBoard(errorMessage);
		},
		
		onJsonPLoadStatuses : function(statuses){
			statuses = jWEIBO.Connection.response.onJsonPLoad(statuses);
			StatusesArray = new Array();
			jQuery(statuses).each(function(i){
				s = jWEIBO.DataObject(this).Status();
				StatusesArray.push(s);
			});
			
			console.log(StatusesArray);
		},
		
		onJsonPLoadComments : function(comments){
			comments = jWEIBO.Connection.response.onJsonPLoad(comments);
			CommentsArray = new Array();
			jQuery(comments).each(function(i){
				c = jWEIBO.DataObject(this).Comment();
				CommentsArray.push(c);
			});
			
			console.log(CommentsArray);
		},
		
		onJsonPLoadCounts : function(counts){
			jWEIBO.Connection.response.onJsonPLoad(counts);
		},
		
		onJsonPLoadCount : function(count){
			jWEIBO.Connection.response.onJsonPLoad(count);
		},
		
		onJsonPLoadStatus : function(status){
			jWEIBO.Connection.response.onJsonPLoad(status);
		},
		
		onJsonPLoadStatus : function(status){
			jWEIBO.Connection.response.onJsonPLoad(status);
		}
	},
	
	request : {
		WEIBOgetJSONP: function( resource, data, callback ) {
			var _callback = "";	
			if (jQuery.isFunction(callback)){
				_callback = callback.toString();
			}else{
				_callback = callback;
				callback = eval(callback);
			}
			jQuery.request.WEIBOajaxSetup(_callback);
			url = jWEIBO.API.HOST + resource + '.' + jWEIBO.API.FORMAT;

			return jQuery.get(url, data, callback,  jWEIBO.API.DATATYPE);
		},
		
		WEIBOpostJSONP: function( resource, data, callback ) {
			// shift arguments if data argument was omited
			if ( jQuery.isFunction( data ) ) {
				type = jWEIBO.API.FORMAT || callback;
				callback = data;
				data = {};
			}
	
			return jQuery.ajax({
				type: "POST",
				url: url,
				data: data,
				success: callback,
				dataType: type
			});
		},
		
		WEIBOajaxSetup: function(jsonpCallback) {
			if (jsonpCallback!=""){
				jWEIBO.Connection.request.WEIBOajaxSettings.jsonpCallback = jsonpCallback;
			}
			jQuery.ajaxSetup(jWEIBO.Connection.request.WEIBOajaxSettings);
		},
		
		WEIBOajaxSettings: {
			dataType : jWEIBO.API.DATATYPE,
			jsonpCallback: "jWEIBO.Connection.response.onJsonPLoad",
			error: function(xhr, textStatus, errorThrown){jWEIBO.Connection.response.onErrorLoad(xhr, textStatus, errorThrown);}
		}
		
	}

});
/*----------------------------------------------------------------------*/
String.prototype.toJSON = function(){
	return JSON.parse(this);
};

(function($){
	getResource = function(request){
		return getAPI(request).resource;
	};
	
	getAPI = function(request){
		return eval(new Array('jWEIBO.API', request.module, request.control).join("."));
	};
	
	parseJSON = function(o){
		dataType = (typeof o).toLowerCase();
		switch(dataType){
			case 'object' : return JSON.stringify(o); break;
			case 'string' : return o.toJSON();
			default : return o;
		}
	};
	
	initRequestModule = function (r, module){
		r.module = module;
		return r;
	};
	initRequestData = function(request){
		request.data = request.data || {};
		APIConfiguration = getAPI(request);
		request.data[APIConfiguration.keyname] = jWEIBO.API.APPKEY;
		return request;
		
	};
	
	$.fn.extend({
		weiboAction: function(module){
			(function(target, module){
				jQuery(target).each(function(){
					requestJSON = parseJSON(jQuery(this).attr('request'));
					requestJSON = initRequestModule(requestJSON, module);
					requestJSON = initRequestData(requestJSON);
					jQuery(this).attr('request', parseJSON(requestJSON));
					jQuery(this).bind("click", function(){
						requestJSON = parseJSON(jQuery(this).attr('request'));
						returnFunction = getAPI(requestJSON).returnFunction;
						jWEIBO.Connection.request.WEIBOgetJSONP(getResource(requestJSON), requestJSON.data, returnFunction);
					});
				});
			})(this, module);
		}   
	});
})(jWEIBO.Controller);

